package com.google.code.v8config;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author yvad
 */
public final class Shell {

    //--------------------------------------------------------------------------
    public static final String SHELL_ARG_HELP = "--help";

    public static final String SHELL_ARG_MODE = "--mode";
    public static final String SHELL_ARG_MODE_PACK = "pack";
    public static final String SHELL_ARG_MODE_UNPACK = "unpack";
    public static final String SHELL_ARG_MODE_RESTORE = "restore";
    
    public static final String SHELL_ARG_INPUT = "--input";
    public static final String SHELL_ARG_OUTPUT = "--output";
    
    public static final String SHELL_ARG_RESTORE_LANG = "--lang";
    
    //--------------------------------------------------------------------------
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
            throws IOException {

        Locale.setDefault(Locale.ENGLISH);
        
        // shell args parsing
        Map<String, String> shellArgs = new HashMap<String, String>();
        Pattern reShellArgs = Pattern.compile("^(--[^=]++)(?:=(.++))?$", 
                Pattern.CASE_INSENSITIVE|Pattern.UNICODE_CASE);
        for(String arg : args) {
            Matcher matcher = reShellArgs.matcher(arg);
            if(matcher.find())
                shellArgs.put(matcher.group(1).toLowerCase(), matcher.group(2));
        }
        
        boolean needHelp = (shellArgs.get(SHELL_ARG_HELP)!=null);
        if(!needHelp) {
            String mode = shellArgs.get(SHELL_ARG_MODE);
            if(mode!=null) {

                String input = shellArgs.get(SHELL_ARG_INPUT);
                String output = shellArgs.get(SHELL_ARG_OUTPUT);

                if(SHELL_ARG_MODE_PACK.compareToIgnoreCase(mode)==0) {

                    if(input==null)
                        needHelp = true;
                    if(input!=null && output==null)
                        output = input + ".cf";
                    
                    if(!needHelp) {
                        Archiver.pack(input, output);
                    }
                    
                } else if(SHELL_ARG_MODE_UNPACK.compareToIgnoreCase(mode)==0) {

                    if(input==null)
                        needHelp = true;
                    if(input!=null && output==null)
                        output = input + ".dir";

                    if(!needHelp) {
                        Archiver.unpack(input, output);
                    }
                    
                } else if(SHELL_ARG_MODE_RESTORE.compareToIgnoreCase(mode)==0) {

                    if(input==null)
                        needHelp = true;
                    
                    String lang = shellArgs.get(SHELL_ARG_RESTORE_LANG);
                    Locale locale = (lang==null ? Locale.getDefault() : new Locale(lang));
                    
                    if(!needHelp) {
                        //Image.restoreSources(input, locale);
                        File dir = new File(input);
                        if(!dir.isDirectory())
                            throw new IOException("Dirname is not directory");

                        ResourceBundle dictionary = Image.getDictionary(locale);
                        Image restorer = new Image();

                        for(File innerDir : dir.listFiles()) {
                            if(!innerDir.isDirectory())
                                continue;

                            for(File file : innerDir.listFiles())
                                if(file.getName().compareToIgnoreCase("image")==0) {
                                    System.out.println(innerDir.getName()+" - restored");
                                    restorer.restoreSource(innerDir, dictionary);
                                    break;
                                }
                        }
                    }
                    
                }

            } else 
                needHelp = true;
        }
        
        // need help
        if(needHelp) {
            System.out.println("java -jar v8Config [ --help | --mode=mode options ]");
            System.out.println("    --help - prints this message.");
            System.out.println("    --mode=pack --input=<dirname> [ --output=<filename> ]");
            System.out.println("    --mode=unpack --input=<filename> [ --output=<dirname> ]");
            System.out.println("    --mode=restore --input=<dirname> [ --lang=[ en | ru ] ]");
        }
        
    }

    //--------------------------------------------------------------------------
}
